package com.inns.position.hbase.task;

import com.inns.position.hbase.StatType;
import com.inns.position.hbase.service.StatisticsService;
import com.inns.position.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//http://cron.qqe2.com/
@Component
@EnableScheduling
public class StatisticsScheduledTask {

    private static final String pattern = "MM月dd日 HH时mm分ss秒";
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private StatisticsService statisticsService;

    private void print(String prefix) {
        System.out.println(prefix + ":" + DateUtil.formatCurrentDate(pattern));
        logger.info(prefix + ":" + DateUtil.formatCurrentDate(pattern));
    }

    @Scheduled(cron = "0/10 * * * * ?")
    public void second() {
//        print("second");
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    public void minute() {
        print("minute");
        long curTime = System.currentTimeMillis();
        StatType statType = StatType.MINUTE;
        long startTime = statType.getStartTime(curTime);
        long endTime = statType.getEndTime(curTime);

        statisticsService.statistics331(statType, startTime, endTime);
        statisticsService.statistics332(statType, startTime, endTime);
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
    public void hour() {
        print("hour");
        long curTime = System.currentTimeMillis();
        StatType statType = StatType.HOUR;
        long startTime = statType.getStartTime(curTime);
        long endTime = statType.getEndTime(curTime);

        statisticsService.statistics331(statType, startTime, endTime);
        statisticsService.statistics332(statType, startTime, endTime);

        statisticsService.statPath(statType, startTime, endTime);
    }

    @Scheduled(cron = "0 0 0 1/1 * ?")
    public void day() {
        print("day");
        long curTime = System.currentTimeMillis();
        StatType statType = StatType.DAY;
        long startTime = statType.getStartTime(curTime);
        long endTime = statType.getEndTime(curTime);

        statisticsService.statistics331(statType, startTime, endTime);
        statisticsService.statistics332(statType, startTime, endTime);
        statisticsService.statistics333(statType, startTime, endTime);
        statisticsService.statistics334(statType, startTime, endTime);
        statisticsService.statistics334_poi(statType, startTime, endTime);
        statisticsService.statPath(statType, startTime, endTime);
    }

    @Scheduled(cron = "0 0 0 ? * MON")
    public void week() {
        print("week");
        long curTime = System.currentTimeMillis();
        StatType statType = StatType.WEEK;
        long startTime = statType.getStartTime(curTime);
        long endTime = statType.getEndTime(curTime);

        statisticsService.statistics331(statType, startTime, endTime);
        statisticsService.statistics332(statType, startTime, endTime);
        statisticsService.statistics333(statType, startTime, endTime);
        statisticsService.statistics334(statType, startTime, endTime);
        statisticsService.statistics334_poi(statType, startTime, endTime);
        statisticsService.statistics335(statType, startTime, endTime);
        statisticsService.statistics335_poi(statType, startTime, endTime);
    }

    @Scheduled(cron = "0 0 0 1 * ?")
    public void month() {
        print("month");
        long curTime = System.currentTimeMillis();
        StatType statType = StatType.MONTH;
        long startTime = statType.getStartTime(curTime);
        long endTime = statType.getEndTime(curTime);

        statisticsService.statistics331(statType, startTime, endTime);
        statisticsService.statistics332(statType, startTime, endTime);
        statisticsService.statistics333(statType, startTime, endTime);
        statisticsService.statistics334(statType, startTime, endTime);
        statisticsService.statistics334_poi(statType, startTime, endTime);
        statisticsService.statistics335(statType, startTime, endTime);
        statisticsService.statistics335_poi(statType, startTime, endTime);
    }
}
